const qiniuUploader = require("../../utils/qiniuUploader");
const { HTTP } = require('../../utils/http');
const { api } = require('../../utils/api');
const { request } = new HTTP();
const app = getApp()

Page({
  data: {
    qiniuToken: '',
    selectPhoto: '',
    categoryId: '',
    categoryTitle: '',
    content: ''
  },
  
  onLoad(options) {
    const { categoryId, categoryTitle } = options;
    this.setData({
      categoryId,
      categoryTitle,
      selectPhoto: app.globalData.selectPhoto
    })
  },

  publish() {
    if (!this.data.content) {
      
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    // 暂时先用中文key 判断
    const actions = {
      '图文': () => {
        this._getToken();
      },
      '文字': () => {
        const params = {
          category_id: this.data.categoryId,
          category_title: this.data.categoryTitle,
          content: this.data.content
        };
        this._themeAdd(params)
      }
    };
    actions[this.data.categoryTitle]();
  },

  bindinputChange(e) {
    this.setData({
      content: e.detail.value
    });
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1
    });
  },

  // 获取七牛上传token
  async _getToken() {
    const { returnCode = null, bean = {} } = await request({
      url: api.getQiniuToken,
      method: 'get',
      data: {}
    });
    if (returnCode === '0000') {
      this.setData({
        qiniuToken: bean.qiniuToken
      });
      this._initQiniu(bean.qiniuToken);
    }
  },

  // 初始化七牛配置
  _initQiniu(token) {
    const options = {
        region: 'ECN',
        // 获取uptoken方法三选一即可，执行优先级为：uptoken > uptokenURL > uptokenFunc。三选一，剩下两个置空。推荐使用uptokenURL，详情请见 README.md
        uptoken: token,
        uptokenURL: '',
        uptokenFunc: function () { },
        domain: 'https://paas.star1024.cn',
        shouldUseQiniuFileName: false
    };
    qiniuUploader.init(options);
    this.uplodQiniu(app.globalData.selectPhoto);
  },

  // 上传七牛
  async uplodQiniu(filePath) {
    wx.showLoading({
      title: '上传中',
    })
    const self = this;
    qiniuUploader.upload(filePath, (res) => {
      const params = {
        themeimage: res.imageURL,
        category_id: this.data.categoryId,
        category_title: this.data.categoryTitle,
        content: this.data.content
      };
      self._themeAdd(params);
    }, (error) => {
      wx.hideLoading();
      wx.showToast({
        title: JSON.stringify(error),
        icon: 'none',
        duration: 2000
      });
      console.error('error: ' + JSON.stringify(error));
    },
    null,
    (progress) => {
      console.log('上传进度', progress.progress);
    }, cancelTask => {
      console.log('🚀 ~ file: index.js ~ line 65 ~ uplodQiniu ~ cancelTask', cancelTask);
    });
  },

  // 新增话题
  async _themeAdd(params) {
    
    const { returnCode = null, bean = {} } = await request({
      url: api.themeAdd,
      method: 'post',
      data: params
    });
    if (returnCode === '0000') {
      wx.hideLoading();
      app.globalData.selectPhoto = ''
      selectPhoto: '',
      wx.navigateBack({
        delta: 2
      });
    }
  }
})
